// pages/profile/profile.js
const app = getApp()
var store = require("../../../utils/storageUtil.js")
const constant = require("../../../constant.js")
const storage = require("../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    img_url:"",
    id: '',
    img: '',
    head: '',
    student: '',
  },
  
  onShareAppMessage: function (res) {
    let gbid = res.target.dataset.info;
    console.log('gbid')
    console.log(res.target.dataset.info)
    return {
      title: '打卡',
      path: '/pages/profile/share_page/share?tgid=' + gbid,
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: "分享成功",
          icon: 'success',
          duration: 2000
        })
      },
      fail: function (res) {
        // 分享失败
      },
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id: options.name,
      img: options.img,
      head: options.head,
      student: options.student,
    })
    console.log(this.data.head);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    
    wx.request({
      url: constant.api(app.globalData.pord).url+'/xh/api/image/',
      method: 'POST',
      data: {
        name: that.data.id,
        c_image: that.data.img,
        head_image: that.data.head,
        student: that.data.student,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        console.log(that.data.id)
        // if (res.data.code == "0") {
        console.log(constant.api(app.globalData.pord).url+'/xh' + res.data)

        that.setData({
          img_url: constant.api(app.globalData.pord).url+'/xh' + res.data,
        });


      },

    })

   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //点击开始的时间  
  timestart: function (e) {
    var _this = this;
    _this.setData({ timestart: e.timeStamp });
  },
  //点击结束的时间
  timeend: function (e) {
    var _this = this;
    _this.setData({ timeend: e.timeStamp });
  },

  //保存图片
  saveImg: function (e) {
    var _this = this;
    console.log(e.currentTarget.dataset.gid)

    var times = _this.data.timeend - _this.data.timestart;
    if (times > 300) {
      console.log("长按");
      wx.showActionSheet({
        itemList: ['保存到本地'],
        success: function (res) {
          console.log(res.tapIndex)
          wx.getSetting({
            success: function (res) {
              wx.authorize({
                scope: 'scope.writePhotosAlbum',
                success: function (res) {
                  console.log("授权成功");
                  console.log(e.currentTarget.dataset.gid);
                  var imgUrl = e.currentTarget.dataset.gid;

                  wx.downloadFile({//下载文件资源到本地，客户端直接发起一个 HTTP GET 请求，返回文件的本地临时路径
                    url: imgUrl,
                    success: function (res) {
                      // 下载成功后再保存到本地
                      wx.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath,//返回的临时文件路径，下载后的文件会存储到一个临时文件
                        success: function (res) {
                          wx.showToast({
                            title: '成功保存到相册',
                            icon: 'success'
                          })
                        }
                      })
                    }
                  })
                }
              })
            }
          })

        },
        fail: function (res) {
          console.log(res.errMsg)
        }
      })
     
    }
  },
  saveImage: function (e) {
    var _this = this;
    console.log(e.currentTarget.dataset.gid)

    // var times = _this.data.timeend - _this.data.timestart;
    // if (times > 300) {
    //   console.log("长按");
    //   wx.showActionSheet({
    //     itemList: ['保存到本地'],
    //     success: function (res) {
    //       console.log(res.tapIndex)
          wx.getSetting({
            success: function (res) {
              wx.authorize({
                scope: 'scope.writePhotosAlbum',
                success: function (res) {
                  console.log("授权成功");
                  console.log(e.currentTarget.dataset.gid);
                  var imgUrl = e.currentTarget.dataset.gid;

                  wx.downloadFile({//下载文件资源到本地，客户端直接发起一个 HTTP GET 请求，返回文件的本地临时路径
                    url: imgUrl,
                    success: function (res) {
                      // 下载成功后再保存到本地
                      wx.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath,//返回的临时文件路径，下载后的文件会存储到一个临时文件
                        success: function (res) {
                          wx.showToast({
                            title: '成功保存到相册',
                            icon: 'success'
                          })
                        }
                      })
                    }
                  })
                }
              })
            }
          })

      //   },
      //   fail: function (res) {
      //     console.log(res.errMsg)
      //   }
      // })

    // }
  },
})